package com.ysd.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.ysd.gulimall.member.vo.MemberLoginVo;
import com.ysd.gulimall.member.vo.MemberRegistVo;
import com.ysd.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ysd.gulimall.member.entity.MemberEntity;
import com.ysd.gulimall.member.service.MemberService;
import com.ysd.common.utils.PageUtils;
import com.ysd.common.utils.R;



/**
 * 会员
 *
 * @author CrazyCat
 * @email CrazyCat@qq.com
 * @date 2020-07-12 14:15:49
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @PostMapping("/oauth2/login")
    public R oauthlogin(@RequestBody SocialUser socialUser) throws Exception {

        MemberEntity memberEntity = memberService.login(socialUser);

        if (memberEntity==null){
            return R.error(001,"账号密码错误");
        }
        return R.ok().setData(memberEntity);
    }


    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){

        MemberEntity memberEntity = memberService.login(vo);

        if (memberEntity==null){
            return R.error(001,"账号密码错误");
        }
        return R.ok().setData(memberEntity);
    }

    @PostMapping("/regist")
    public R regist (@RequestBody MemberRegistVo vo){
        try {
            System.out.println("membervo:"+vo);
            MemberEntity memberEntity = new MemberEntity();
            memberEntity.setLevelId(1L);
            memberEntity.setUsername(vo.getUserName());
            memberEntity.setPassword(vo.getPassword());
            memberEntity.setMobile(vo.getPhone());
            memberService.save(memberEntity);
            return R.ok();
        }catch (Exception e){
            return R.error("注册失败");
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
